// pages/find/fitness/detail/detail.js
import { HTTP } from '../../../../utils/http.js'
let ajax = new HTTP()
var app = getApp()

Page({
    data: {
        detailTitle: "",
        detailId: "",
        winHeight: "",
        posterSrc: "",
        videoSrc: "",
        isLove: 0,
        loveNum: 0,
        prize: 1088,
        buyFlag: 1,
        isBuy: 1,
        title: "",
        content: "",
        hastest: 2,
        testtime: 0,
        objFit: "fill",
        playFlag: false,
        testEnd: false,
        hintText: false,
        loading: false
    },
    onBindPlay () {
        this.setData({
            objFit: "contain"
        })
    },
    // 播放
    playVideo () {
        this.objFit = "contain"
        // 判断是否付费
        if (this.data.buyFlag == 1) {
            // 可以试看
            if (this.data.hastest == 1) {
                this.videoContext.play()
                this.setData({
                    playFlag: true,
                    hintText: true
                })
                setTimeout(() => {
                    this.setData({
                        hintText: false
                    })
                }, 5000)
            } else {
                wx.showModal({
                    title: '',
                    content: '该视频购买之后才能观看！',
                    showCancel: false
                })
            }
        }
    },
    onUpdate (event) {
        let currentTime = Math.floor(event.detail.currentTime)
        // 允许试看
        if (this.data.buyFlag == 1 && this.data.hastest == 1) {
            if (currentTime >= this.data.testtime) {
                this.videoContext.pause()
                this.setData({
                    playFlag: false,
                    testEnd: true
                })
            }
        }
    },
    // 喜欢
    onLoveTap(event) {
        let isLove
        let loveNum = this.data.loveNum
        if (this.data.isLove === 1) {
            // 取消收藏        
            isLove = 0
            loveNum--
        } else {
            // 收藏
            isLove = 1
            loveNum++
        }
        wx.showToast({
            title: isLove === 1 ? '收藏成功' : '取消成功',
            icon: "success",
            duration: 1500
        })
        this._getLoveInfo(this.data.detailId, app.globalData.memberid)
        this.setData({
            isLove: isLove,
            loveNum: loveNum
        })
    },
    // 购买视频
    onBuyTab (event) {
        this.videoContext.exitFullScreen()
        wx.navigateTo({
            url: 'buy/buy?id=' + this.data.detailId,
        })
    },
    onReady (res) {
        this.videoContext = wx.createVideoContext('myVideo')
    },
    onLoad(options) {
        wx.showLoading({
            title: '加载中',
        })
        this.setData({
            detailTitle: options.title,
            detailId: options.id
        })
        this._getDetailList(app.globalData.memberid, options.id)
        wx.setNavigationBarTitle({
            title: options.title
        })
        // 获取屏幕高度
        wx.getSystemInfo({
            success: (res) => {
                let clientHeight = res.windowHeight
                let clienWidth = res.windowWidth
                let rpxR = 750 / clienWidth
                let calc = clientHeight * rpxR
                this.setData({
                    winHeight: calc - 540
                })
            },
        })
    },
    // 转发 
    onShareAppMessage: function () {

    },
    // 获取信息
    _getDetailList (memberid, id) {
        ajax.request({
            url: "trainlist/read",
            data: {
                memberid: memberid,
                id: id
            },
            success: (res) => {
                res = res.data
                wx.hideLoading()
                this.setData({
                    posterSrc: res.pic,
                    videoSrc: res.videourl,
                    buyFlag: res.isfree,
                    prize: res.trainfee,
                    title: res.title,
                    content: res.content,
                    isBuy: res.hassub,
                    loveNum: res.collectnum,
                    isLove: res.hascol,
                    hastest: res.hastest,
                    testtime: res.testtime,
                    loading: true
                })
                // 如果是免费视频
                if (this.data.buyFlag == 2) {
                    this.setData({
                        playFlag: true
                    })
                    // this.videoContext.play()
                }
            }
        })
    },
    // 请求收藏
    _getLoveInfo(id, memberid) {
        ajax.request({
            url: "collectlist",
            method: "POST",
            data: {
                reltype: 3,
                relid: id,
                memberid: memberid
            },
            success: (res) => {
                console.log(res)
            }
        })
    }
})